/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mb.bill.ui;

import com.mb.main.AppManager;
import com.mb.main.MyDefaultTableCellRenderer;
import com.mb.main.MyDefaultTableModel;
import com.mb.main.MyTable;
import com.mb.utils.ITableColumnString;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ldanh
 */
public class CreateBillFormDialog extends javax.swing.JDialog implements ITableColumnString{

    private AppManager app;
    private final String[] colsName = {STT, MA, TEN, SO_LUONG, GIA_BAN};
    private DefaultTableModel tableModelBill;
    private MyTable tableBill;
    /**
     * Creates new form CreateBillFormDialog
     */
    public CreateBillFormDialog(java.awt.Frame parent, boolean modal, AppManager app) {
        super(parent, modal);
        this.setTitle("Tạo đơn hàng");
        this.app = app;
        initComponents();
        renamePopupMenuItem();
        tableModelBill = new MyDefaultTableModel();
        tableModelBill.setColumnIdentifiers(colsName);
        tableModelBill.addRow(new String[]{"a","b","c","0","0"});
        tableBill = new MyTable(tableModelBill);
        tableBill.setDefaultRenderer(String.class, new MyDefaultTableCellRenderer());
        tableBill.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON3) {
                    changedMenuItemState(tableBill.getSelectedRowCount() > 0);
                    popupMenu.show(tableBill, e.getX(), e.getY());
                }
            }
            
        });
        panelCenter.removeAll();
        JScrollPane scrollPaneBill = new JScrollPane(tableBill);
        panelCenter.add(scrollPaneBill);
        scrollPaneBill.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON3) {
                    changedMenuItemState(tableBill.getSelectedRowCount() > 0);
                    popupMenu.show(scrollPaneBill, e.getX(), e.getY());
                }
            }
            
        });
    }

    private void renamePopupMenuItem() {
        menuItemAdd.setText("Thêm sản phẩm");
        menuItemEdit.setText("Sửa thông tin");
        menuItemDelete.setText("Xóa");
    }
    
    private void changedMenuItemState(boolean isEnabled) {
        menuItemEdit.setEnabled(isEnabled);
        menuItemDelete.setEnabled(isEnabled);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popupMenu = new javax.swing.JPopupMenu();
        menuItemAdd = new javax.swing.JMenuItem();
        menuItemEdit = new javax.swing.JMenuItem();
        menuItemDelete = new javax.swing.JMenuItem();
        panelTop = new javax.swing.JPanel();
        lbCustomerName = new javax.swing.JLabel();
        tfCustomerName = new javax.swing.JTextField();
        lbCustomerPhone = new javax.swing.JLabel();
        tfCustomerPhone = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        tfDeliveryDate = new javax.swing.JLabel();
        cbDay = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cbMonth = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cbYear = new javax.swing.JComboBox();
        chbIsDelivery = new javax.swing.JCheckBox();
        panelCenter = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        tfTotal = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();

        menuItemAdd.setText("jMenuItem1");
        popupMenu.add(menuItemAdd);

        menuItemEdit.setText("jMenuItem1");
        popupMenu.add(menuItemEdit);

        menuItemDelete.setText("jMenuItem1");
        popupMenu.add(menuItemDelete);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        panelTop.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin khách mua"));

        lbCustomerName.setText("Họ tên:");

        lbCustomerPhone.setText("Điện thoại:");

        jLabel1.setText("Địa chỉ:");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        tfDeliveryDate.setText("Ngày giao hàng:");
        tfDeliveryDate.setEnabled(false);

        cbDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbDay.setEnabled(false);

        jLabel3.setText("/");

        cbMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbMonth.setEnabled(false);

        jLabel4.setText("/");

        cbYear.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbYear.setEnabled(false);

        chbIsDelivery.setText("Giao hàng");
        chbIsDelivery.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chbIsDeliveryActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelTopLayout = new javax.swing.GroupLayout(panelTop);
        panelTop.setLayout(panelTopLayout);
        panelTopLayout.setHorizontalGroup(
            panelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTopLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelTopLayout.createSequentialGroup()
                        .addComponent(tfDeliveryDate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(chbIsDelivery))
                    .addGroup(panelTopLayout.createSequentialGroup()
                        .addGroup(panelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbCustomerName)
                            .addComponent(jLabel1))
                        .addGap(44, 44, 44)
                        .addGroup(panelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 556, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(panelTopLayout.createSequentialGroup()
                                .addComponent(tfCustomerName, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(lbCustomerPhone)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tfCustomerPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(156, Short.MAX_VALUE))
        );
        panelTopLayout.setVerticalGroup(
            panelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelTopLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(panelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbCustomerName)
                    .addComponent(tfCustomerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCustomerPhone)
                    .addComponent(tfCustomerPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 11, Short.MAX_VALUE)
                .addGroup(panelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfDeliveryDate)
                    .addComponent(cbDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(cbMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(cbYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chbIsDelivery))
                .addContainerGap())
        );

        getContentPane().add(panelTop, java.awt.BorderLayout.NORTH);

        panelCenter.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách sản phẩm"));
        panelCenter.setLayout(new java.awt.GridLayout());

        jTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable);

        panelCenter.add(jScrollPane2);

        getContentPane().add(panelCenter, java.awt.BorderLayout.CENTER);

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel7.setText("Tổng tiền:");
        jPanel1.add(jLabel7);

        tfTotal.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        tfTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfTotal.setText("0");
        tfTotal.setPreferredSize(new java.awt.Dimension(300, 28));
        jPanel1.add(tfTotal);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel5.setText("VND");
        jPanel1.add(jLabel5);

        jButton1.setText("Lưu");
        jButton1.setPreferredSize(new java.awt.Dimension(70, 23));
        jPanel1.add(jButton1);

        btCancel.setText("Hủy");
        btCancel.setPreferredSize(new java.awt.Dimension(70, 23));
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });
        jPanel1.add(btCancel);

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void chbIsDeliveryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chbIsDeliveryActionPerformed
        // TODO add your handling code here:
        boolean isDelivery = chbIsDelivery.isSelected();
        tfDeliveryDate.setEnabled(isDelivery);
        cbDay.setEnabled(isDelivery);
        cbMonth.setEnabled(isDelivery);
        cbYear.setEnabled(isDelivery);
    }//GEN-LAST:event_chbIsDeliveryActionPerformed

    private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btCancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btCancel;
    private javax.swing.JComboBox cbDay;
    private javax.swing.JComboBox cbMonth;
    private javax.swing.JComboBox cbYear;
    private javax.swing.JCheckBox chbIsDelivery;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel lbCustomerName;
    private javax.swing.JLabel lbCustomerPhone;
    private javax.swing.JMenuItem menuItemAdd;
    private javax.swing.JMenuItem menuItemDelete;
    private javax.swing.JMenuItem menuItemEdit;
    private javax.swing.JPanel panelCenter;
    private javax.swing.JPanel panelTop;
    private javax.swing.JPopupMenu popupMenu;
    private javax.swing.JTextField tfCustomerName;
    private javax.swing.JTextField tfCustomerPhone;
    private javax.swing.JLabel tfDeliveryDate;
    private javax.swing.JTextField tfTotal;
    // End of variables declaration//GEN-END:variables
}
